﻿using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.UI;

/* ***********************************************
 * Describe:录音显示界面
 * Author :  zhaozhongyang
 * Email: 1148312315@qq.com
 * DATA: 2017/7/28 16:00:21 
 * FileName: ChatPageManager 
 * Version: V1.0.1
 * ***********************************************/
public class PageManager : MonoBehaviour ,IPointerDownHandler, IPointerUpHandler {

    private static PageManager instance = null;
    public static PageManager GetInstance()
    {
        return instance;
    }

    private Transform root;
    private GameObject addNode;
    private Button Voice_Btn;
    private Transform ban;
    private AudioSource aud;
    private bool isban = false;

    void Start ()
    {
        instance = this;
        root = GameObject.Find("Canvas").transform;
        aud = GetComponent<AudioSource>();
        Voice_Btn = root.FindChild("addNode/GameRoomPage(Clone)/btnGroup/VoiceBtn").GetComponent<Button>();
	    addNode = root.FindChild("addNode/GameRoomPage(Clone)/FightAddNode").gameObject;
        ban = Voice_Btn.gameObject.transform.FindChild("Ban");
    }
	
	void Update ()
	{
	    if (!isban)
	    {
            OnButtonHandled();
        }   
	}
   //0 关闭  1开启
    public void ShowBan( int controller)
    {
        //switch (controller)
        //{
        //    case 0:
        //        isban = false;
        //        ban.gameObject.SetActive(false);
        //        Voice_Btn.enabled = true;
        //        break;

        //    case 1:
        //        isban = true;
        //        ban.gameObject.SetActive(true);
        //        Voice_Btn.enabled = false;
        //        break;
        //}
    }

    private float delay = 0.2f;
    private bool isDown = false;
    private float lastIsDownTime;

    private void OnButtonHandled()
    {
        if (isDown) {
            for (int i = 0; i < Microphone.devices.Length; i++)
            {
                if (Microphone.IsRecording(Microphone.devices[i]))
                {
                    return;
                }
            }

            MicphoneSoundRecording.GetInstance().StartRecord();
            isDown = false;
        }
    }

    bool isTruePointerDown = false;
    public void OnPointerDown(PointerEventData eventData) {

        //if (aud.isPlaying)
        //{
        //    return;
        //}

        isDown = true;
        lastIsDownTime = Time.time;

        Debug.Log("ButtonDown");
    }

    public void OnPointerUp(PointerEventData eventData) {

        //if (aud.isPlaying)
        //{
        //    return;
        //}

        MicphoneSoundRecording.GetInstance().StopRecord();

        isDown = false;
    }


    public void showRecordPage()
    {
        GameObject obj = TooL.LoadPrefab(root.FindChild("addNode/GameRoomPage(Clone)/FightAddNode").gameObject, "RecordPage").transform.FindChild("Image").gameObject;
        obj.AddComponent<CountDownPage>();
    }

    public void DestoryRecordPage()
    {
        if (addNode.transform.childCount != 0)
        {
            Destroy(addNode.transform.FindChild("RecordPage").gameObject);
        }     
    }
} 








